//
//  qiaoDanViewController.m
//  FastRunDemo
//
//  Created by 苟亚明 on 16/9/16.
//  Copyright © 2016年 苟亚明. All rights reserved.
//

#import "qiaoDanViewController.h"
#import "MenuTestController.h"
#import "XCPullMenu.h"
#import "squareRightMenuView.h"
@interface qiaoDanViewController () <XCPullMenuDelegate,UIScrollViewDelegate>
/** 标签栏的scrollView*/
@property (nonatomic, strong) UIScrollView *titleView;
/** 标签栏的按钮 */
@property (nonatomic, weak) UIButton *titleBtn;
/** 标签栏底部红色指示器*/
@property (nonatomic, weak) UIView *indicatorView;
/** 底部的所有内容 */
@property (nonatomic, weak) UIScrollView *contentView;
/** 下拉菜单*/
@property (nonatomic, strong) XCPullMenu *menu;
/** 订单数组*/
@property (nonatomic, strong) NSArray *arrayMenu;
/**记录当前查看的标签对应的视图*/
@property (nonatomic,copy)NSString * catId;
/** 右滑菜单*/
@property (nonatomic, strong) squareRightMenuView *rightMenuView;

@end

@implementation qiaoDanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavi];
    
    // 模拟数据
    self.arrayMenu = @[@"全部订单",@"帮你买",@"带饭",@"拿快递",@"电脑维修",@"你懂的",@"物品求租",@"学习帮辅",@"洗衣服"];
    self.view.backgroundColor = [UIColor colorWithRed:224/255.0 green:224/255.0 blue:224/255.0 alpha:1];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setupChildVcWithData:self.arrayMenu];
    [self setupTagWithData:self.arrayMenu];
    [self setupScrollContentView];
    
    
}
/**
 * 设置导航栏
 */
- (void)setupNavi{
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"square_navi_message" highImage:@"square_navi_message" target:self action:@selector(leftBtnClick)];
    
    
}


- (void)leftBtnClick{
    
    __weak __typeof(self) weakSelf = self;
    if (!_menu) {
        
        XCPullMenu *pullMenu = [[XCPullMenu alloc]initWithDataArr:@[@"我的好友"] origin:CGPointMake(0, 64) width:120 rowHeight:44];
        pullMenu.delegate = self;
        self.menu = pullMenu;
        _menu.dismiss = ^() {
            weakSelf.menu = nil;
        };
        //    [[UIApplication sharedApplication].keyWindow.window addSubview:pullMenu];
        
        GYMLog(@"leftBtnClick");
        [self.view addSubview:pullMenu];
    }else{
        [_menu dismissWithCompletion:^(XCPullMenu *object) {
            weakSelf.menu = nil;
        }];
    }
}

/**
 * 设置标签栏
 */
- (void)setupTagWithData:(NSArray *)data{
    // 容器view
    UIScrollView *contentView = [[UIScrollView alloc]init];
    
    

    contentView.x = 0;
    contentView.width = self.view.width;
    contentView.height = 35;
    contentView.y = 64;
    contentView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
    contentView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:contentView];
    self.titleView = contentView;
    
    // 橙色的指示器
    UIView *indicatorView = [[UIView alloc]init];
    indicatorView.height = 2;
    indicatorView.tag = -1;
    indicatorView.y = contentView.height - indicatorView.height;
    indicatorView.backgroundColor = [UIColor orangeColor];
    self.indicatorView = indicatorView;
    
    
    // 画线
    UIView *shortlineLayer = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(contentView.frame), GYMScreenW, 0.5)];
    shortlineLayer.backgroundColor = [UIColor lightGrayColor];
    [self.view bringSubviewToFront:shortlineLayer];
    UIView *shortlineLayer1 = [[UIView alloc]initWithFrame:CGRectMake(0,64,GYMScreenW, 0.5)];
    shortlineLayer1.backgroundColor = [UIColor lightGrayColor];
    [self.view bringSubviewToFront:shortlineLayer1];

    [self.view addSubview:shortlineLayer];
    
    CGFloat btnLast = 0;
    CGFloat btnY = 0;
    CGFloat btnW = 80;
    CGFloat btnH = contentView.height;
    for (NSInteger i = 0; i < data.count; i++) {
        UIButton *btn = [[UIButton alloc]init];
        
        [btn setTitle:data[i] forState:UIControlStateNormal];
        
        CGFloat btnX = btnLast + 15;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        btn.tag = i;
        btn.flog = i;
//        XCZSZXCatModel *model = data[i];
        
        [btn sizeToFit];
        btnLast = CGRectGetMaxX(btn.frame);
        
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateDisabled];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [contentView addSubview:btn];
        
        // 默认选中第一个按钮
//        if (!_catId) {
            if (i == 0) {
                btn.enabled = NO;
                self.titleBtn = btn;
                
                [btn.titleLabel sizeToFit];
                self.indicatorView.width = btn.titleLabel.width;
                self.indicatorView.centerX = btn.centerX;
            }
        
    }
    
    
    contentView.contentSize = CGSizeMake(btnLast + 15, 0);
    [contentView addSubview:indicatorView];
}
/**
 * 底部的scrollView
 */
- (void)setupScrollContentView{
    
    UIScrollView *contentView = [[UIScrollView alloc]init];
    contentView.frame = CGRectMake(0, CGRectGetMaxY(self.titleView.frame), GYMScreenW, GYMScreenH - self.tabBarController.tabBar.height - CGRectGetMaxY(self.titleView.frame));
    contentView.delegate = self;
    contentView.pagingEnabled = YES;
    contentView.showsHorizontalScrollIndicator =YES;
    contentView.showsVerticalScrollIndicator = NO;
    [self.view insertSubview:contentView atIndex:0];
    contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count,0);

    
    self.contentView = contentView;
   
    
    [self scrollViewDidEndScrollingAnimation:contentView];
  
    
}

/**
 * 标签栏按钮点击事件
 */
- (void)btnClick:(UIButton *)button{
    
    // 修改按钮状态
    self.titleBtn.enabled = YES;
    button.enabled = NO;
    self.titleBtn = button;
    
    // 动画
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.width = button.titleLabel.width;
        self.indicatorView.centerX = button.centerX;
    }];
    // 滚动
    CGPoint offset = self.contentView.contentOffset;
    offset.x = button.flog * self.contentView.width;
    [self.contentView setContentOffset:offset animated:YES];
    
    
}

/**
 * setupChildVc
 */
- (void)setupChildVcWithData:(NSArray *)data
{
    for (int i = 0; i < data.count; i++) {
//        XCZSZXCatModel * model = data[i];
//        BOOL flog = 0;
//        for (XCGYMViewController *vc in _myChildControllerArray) {
//            if ([vc.catID isEqualToString: model.catid]) {
//                [self addChildViewController:vc];
//                flog = 1;
//            }
//        }
//        if (flog ==0) {
//            XCGYMViewController *vc = [[XCGYMViewController alloc]init];
//            vc.title = model.catname;
//            vc.catID = model.catid;
//            [self addChildViewController:vc];
//        }
        MenuTestController *vc = [[MenuTestController alloc]init];
        vc.title = data[i];
        [self addChildViewController:vc];
    }
//    [_myChildControllerArray removeAllObjects];
//    [_myChildControllerArray addObjectsFromArray:self.childViewControllers];
}
/**
 * scrollView结束了滚动动画以后就会调用这个方法（比如- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;方法执行的动画完毕后）
 */
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    // 一些临时变量
    CGFloat width = scrollView.frame.size.width;
    CGFloat height = scrollView.frame.size.height;
    CGFloat offsetX = scrollView.contentOffset.x;
    
    // 当前位置需要显示的控制器的索引
    NSInteger index = offsetX / width;
    
    // 点击下titleButton
    for (UIView *btn in self.titleView.subviews) {
        if (btn.flog == index && [btn isKindOfClass:[UIButton class]]) {
            [self btnClick:(UIButton *)btn];
            CGPoint titleOffset = self.titleView.contentOffset;
            titleOffset.x = btn.center.x - width * 0.5;
            // 左边超出处理
            if (titleOffset.x < 0) titleOffset.x = 0;
            // 右边超出处理
            CGFloat maxTitleOffsetX = self.titleView.contentSize.width - width;
            if (titleOffset.x > maxTitleOffsetX) titleOffset.x = maxTitleOffsetX;
            [self.titleView setContentOffset:titleOffset animated:YES];
        }
    }
    
    // 取出需要显示的控制器
    MenuTestController *willShowVc = (MenuTestController *)self.childViewControllers[index];

    // 如果当前位置的位置已经显示过了，就直接返回
    if ([willShowVc isViewLoaded]) return;
    //    if (willShowVc.view.superview) return;
    
    // 添加控制器的view到contentScrollView中;
    willShowVc.view.frame = CGRectMake(offsetX, 0, width, height);
    [scrollView addSubview:willShowVc.view];
}
/**
 * 手指松开scrollView后，scrollView停止减速完毕就会调用这个
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

#pragma mark - <XCPullMenuDelegate>
- (void)XCPullMenu:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GYMLog(@"点击了%zd",indexPath.row);
}


@end
